package edu.sias.course.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;




import edu.sias.course.domain.dto.CourseDTO;
import edu.sias.course.domain.entity.CoursePageQueryParams;
import edu.sias.course.domain.entity.PageResult;
import edu.sias.course.domain.entity.QueryPageBean;
import edu.sias.course.domain.pojo.Catalog;
import edu.sias.course.domain.pojo.Course;
import edu.sias.course.domain.pojo.Tag;

import edu.sias.course.domain.vo.CourseAddVO;
import edu.sias.course.domain.vo.CourseAllVO;
import edu.sias.course.domain.vo.CourseVO;
import edu.sias.course.mapper.CatalogMapper;
import edu.sias.course.mapper.CourseMapper;
import edu.sias.course.mapper.TagMapper;
import edu.sias.course.service.CourseService;
import edu.sias.common.exception.base.BaseException;
import edu.sias.common.utils.DateUtils;
import edu.sias.common.utils.Threads;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description CourseServiceImpl
 * @Author songyu
 * @Date 2023-08-27 07:59
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private CatalogMapper catalogMapper;

    @Resource
    private TagMapper tagMapper;

    /**
     * 添加学科
     *
     * @param courseAddVO
     */
    @Override
    public void add(CourseAddVO courseAddVO) {
        //实例学科
        Course course = new Course();
        //将courseAddVO赋值给course
        BeanUtils.copyProperties(courseAddVO,course);
        //设置创建日期
        course.setCreateDate(DateUtils.dateTime(new Date()));
        //设置创建用户
        course.setUserId(Threads.getCurrentId());
        //调用mapper添加学科
        courseMapper.insert(course);
    }

    /**
     * 分页查询学科
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findListByPage(QueryPageBean<CoursePageQueryParams> queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<CourseVO> courseList =  courseMapper.findByCondition(queryPageBean.getQueryParams());
        Page<CourseVO> page = (Page<CourseVO>) courseList;
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 学科更新方法
     *
     * @param courseDTO
     */
    @Override
    public void update(CourseDTO courseDTO) {
        //1.将courseDTO转换为Course
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO,course);

        //2.更新操作
        courseMapper.update(course);
    }

    /**
     * 删除学科
     *
     * @param courseId
     */
    @Override
    public void delete(Integer courseId) {
        //1.判断当前学科是否有学科目录
       // Integer count = catalogMapper.selectCountCatalogById(courseId);
        /*if(count>0){
            throw new BaseException("有关联的学科目录无法删除");
        }*/
        //2.判断当前学科是否有标签
       /* count = tagMapper.selectCountTagByCourseId(courseId);
        if(count>0){
            throw new BaseException("有关联的学科标签无法删除");
        }*/
        //3.以上都没有删除学科
        courseMapper.deleteById(courseId);
    }

    /**
     * 获取全部学科(含标签、目录列表)列表
     *
     * @return
     */
    @Override
    public List<CourseAllVO> findListAll() {

        //查询所有学科
        List<Course> courseList = courseMapper.findListAll();

        //将 List<Course> 转换为 List<CourseAllVO>
        List<CourseAllVO> courseAllVOList = courseList.stream().map(course -> {

            //实例对象
            CourseAllVO courseAllVO = new CourseAllVO();
            //将course对象属性赋值给courseAllVO中相同的属性
            BeanUtils.copyProperties(course,courseAllVO);
            //根据courseId查询学科目录列表
            List<Catalog> catalogList = catalogMapper.findListByCourseId(course.getId());
            //根据courseId查询学科标签列表
            List<Tag> tagList = tagMapper.findListByCourseId(course.getId());

            //将标签集合和学科目录集合封装到CourseAllVO对象中
            courseAllVO.setCatalogList(catalogList);
            courseAllVO.setTagList(tagList);

            return courseAllVO;
        }).collect(Collectors.toList());

        return courseAllVOList;
    }
    /**
     * 根据id查询学科
     *
     * @param courseId
     * @return
     */
    @Override
    public CourseDTO findById(Integer courseId) {

        Course course = courseMapper.findById(courseId);
        CourseDTO courseDTO = new CourseDTO();
        BeanUtils.copyProperties(course,courseDTO);
        return courseDTO;

    }
}
